<template>
  <el-container class="layout-container-demo" style="height: 100%">
    <el-aside width="200px">
      <el-scrollbar>
        <el-menu :default-openeds="menuIndex" @select="selectHandler" :default-active="defaultPath">
          <component v-for="(item, index) in menuModel.menu" :key="index"
            :is="item.children ? 'el-sub-menu' : 'el-menu-item'" :index="item.route"
            v-show="(item.roleid === 1 && userInfo.role === 'admin') || item.roleid === 0">
            <template #title>
              <el-icon>
                <component :is="item.icon"></component>
              </el-icon>
              {{ item.name }}
            </template>
            <el-menu-item v-for="(items, indexs) in item.children" :key="indexs" :index="items.route"
              v-show="(item.roleid === 1 && userInfo.role === 'admin') || item.roleid === 0">
              <el-icon>
                <component :is="items.icon"></component>
              </el-icon>
              {{ items.name }}
            </el-menu-item>
          </component>
        </el-menu>
      </el-scrollbar>
    </el-aside>

    <el-container>
      <el-header style="text-align: right; font-size: 12px">
        <div class="toolbar">
          <el-dropdown trigger="click" @command="commandHandler">
            <div class="user-info">
              <el-avatar :size="20" :src="userInfo.avator" />
              <span>{{ userInfo.nickname }}</span>
            </div>
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item command="logout">退出登录</el-dropdown-item>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
        </div>
      </el-header>

      <el-main>
        <el-scrollbar>
          <router-view></router-view>
        </el-scrollbar>
      </el-main>
    </el-container>
  </el-container>
</template>

<script setup>
import { computed, ref } from 'vue'
import { useUserStore } from '@/stores/user';
import { storeToRefs } from 'pinia';
import menuModel from '@/model/menuModel';
import router from '@/router';
const store = useUserStore()
const { userInfo, defaultPath } = storeToRefs(store)
// 退出登录
function commandHandler(item) {
  if (item === 'logout') store.clearUserInfo()
}
// 默认全部展开
const menuIndex = computed(() => menuModel.menu.filter((item) => item.children).map((item) => item.route))
// 点击跳转
function selectHandler(index) {
  router.push(index)
  defaultPath.value = index
}
// 如果默认路径存在直接跳转到该路径
if (store.defaultPath) router.push(defaultPath.value)

</script>

<style scoped>
.layout-container-demo .el-header {
  position: relative;
  background-color: var(--el-color-primary-light-7);
  color: var(--el-text-color-primary);
}

.layout-container-demo .el-aside {
  color: var(--el-text-color-primary);
  background: var(--el-color-primary-light-8);
}

.layout-container-demo .el-menu {
  border-right: none;
}

.layout-container-demo .el-main {
  padding: 0;
}

.layout-container-demo .toolbar {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  height: 100%;
  right: 20px;
}

.user-info {
  width: 80px;
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>
